package exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.bean.PatentInfo;
import exam.bean.UserInfo;
import exam.bean.UtilFiles;
import exam.dao.PatentInfoDao;
import exam.dao.UserInfoDao;
import exam.dao.UtilFileDao;
import exam.service.IPatentInfoService;
import exam.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatentInfoServiceImpl implements IPatentInfoService {
    @Autowired
    PatentInfoDao patentInfoDao;
    @Autowired
    UtilFileDao utilFileDao;

    @Autowired
    UserInfoDao userInfoDao;

    @Override
    public int insertInfo(PatentInfo patentInfo,String upLoadPath) {
        patentInfo.setEnterTime(new Date());
        Integer re =  patentInfoDao.insertInfo(patentInfo);
        Integer fileRe = 0;
        //多文件上传
        if(patentInfo.getFiles() != null){
            for (MultipartFile file: patentInfo.getFiles()) {
                UtilFiles utilFiles = new UtilFiles();
                utilFiles.setUtilAddress(upLoadPath);
                utilFiles.setUtilFileType(file.getContentType());
                utilFiles.setUtilName(FileUtil.upLoadFileSingle(file,upLoadPath));
                utilFiles.setUtilType(3);
                utilFiles.setDataId(patentInfo.getId());
                utilFiles.setUtilSrcName(file.getOriginalFilename());
                fileRe += utilFileDao.insertInfo(utilFiles);
            }
        }
        return re + fileRe;
    }

    @Override
    public int updateInfo(PatentInfo patentInfo,String upLoadPath) {
        Integer fileRe = 0;
        if(patentInfo.getFiles() != null){
            for (MultipartFile file: patentInfo.getFiles()) {
                UtilFiles utilFiles = new UtilFiles();
                utilFiles.setUtilAddress(upLoadPath);
                utilFiles.setUtilFileType(file.getContentType());
                utilFiles.setUtilName(FileUtil.upLoadFileSingle(file,upLoadPath));
                utilFiles.setUtilType(3);
                utilFiles.setDataId(patentInfo.getId());
                utilFiles.setUtilSrcName(file.getOriginalFilename());
                fileRe += utilFileDao.insertInfo(utilFiles);
            }
        }
        return patentInfoDao.updateInfo(patentInfo) + fileRe;
    }

    @Override
    public int deleteInfo(int id) {
        return patentInfoDao.deleteInfo(id);
    }

    @Override
    public PageInfo<PatentInfo> selectInfo(int pageNum, int pageSize,Map<String,Object> map) {

        UserInfo userInfo = getCurrentUserInfo();
        //查询属于自己的数据 ---> 管理员除外
        if(userInfo.getUserGroupId() != 1 ){
            map.put("enterUserId",userInfo.getId());
        }


        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(patentInfoDao.selectInfo(map));
    }

    @Override
    public PatentInfo selectInfoSingle(Map<String, Object> map) {

        PatentInfo patentInfo = patentInfoDao.selectInfo(map).get(0);
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("utilType",3);
        queryMap.put("dataId",map.get("id"));
        patentInfo.setUtilFiles(utilFileDao.selectInfo(queryMap));
        return patentInfo;
    }

    public UserInfo getCurrentUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        Map<String, Object> map = new HashMap<>();
        map.put("name",authenticationToken.getPrincipal());
        return userInfoDao.selectInfo(map).get(0);
    }
}
